<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="singleDatePicker">
          <el-form-item prop="checkDate" label="盘点日期" size="small">
            <el-date-picker
              v-model="searchForm.checkDate"
              type="date"
              value-format="timestamp"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <!--<el-col class="other">-->
          <!--<el-form-item label="部门" size="small">-->
            <!--<el-input type="text" v-model="searchForm.teamName"></el-input>-->
          <!--</el-form-item>-->
        <!--</el-col>-->
        <el-col class="other">
          <el-form-item label="使用人" size="small">
            <el-input type="text" v-model="searchForm.user"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="办公桌" size="small">
            <el-input type="text" v-model="searchForm.officeTable"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="办公椅" size="small">
            <el-input type="text" v-model="searchForm.officeChair"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="显示器" size="small">
            <el-input type="text" v-model="searchForm.displayer"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="主机" size="small">
            <el-input type="text" v-model="searchForm.computerMain"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="购买年份" size="small">
            <el-input type="number" v-model.number="searchForm.computerBuy"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>

      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
      @openVisible="openVisible"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.assetsId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
        <el-form-item prop="checkDate" label="盘点日期" size="small">
          <el-date-picker
            v-model="teamRow.checkDate"
            type="date"
            value-format="timestamp"
            placeholder="选择日期"
            style="width: 180px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="user" label="使用人" size="small">
          <el-autocomplete
            style="width: 190px"
            v-model="teamRow.user"
            :fetch-suggestions="querySearchUser"
            @select="handleSelectUser"
            placeholder="请输入内容"
          >
          </el-autocomplete>
        </el-form-item>
        <el-form-item v-if="teamRow.teamName" label="部门" size="small">
          {{teamRow.teamName}}
        </el-form-item>
        <el-form-item prop="officeTable" label="办公桌" size="small">
          <el-input type="text" v-model="teamRow.officeTable"></el-input>
        </el-form-item>
        <el-form-item prop="officeChair" label="办公椅" size="small">
          <el-input type="text" v-model="teamRow.officeChair"></el-input>
        </el-form-item>
        <el-form-item label="显示器" size="small">
          <el-input type="text" v-model="teamRow.displayer"></el-input>
        </el-form-item>
        <el-form-item label="显示器" size="small">
          <el-input type="text" v-model="teamRow.displayerTwo"></el-input>
        </el-form-item>
        <el-form-item label="主机" size="small">
          <el-input type="text" v-model="teamRow.computerMain"></el-input>
        </el-form-item>
        <el-form-item label="系统版本" size="small">
          <el-input type="text" v-model="teamRow.systemVersion"></el-input>
        </el-form-item>
        <el-form-item label="内存" size="small">
          <el-input type="text" v-model="teamRow.memory"></el-input>
        </el-form-item>
        <el-form-item label="硬盘" size="small">
          <el-input type="text" v-model="teamRow.hd"></el-input>
        </el-form-item>
        <el-form-item label="CPU" size="small">
          <el-input type="text" v-model="teamRow.cpu"></el-input>
        </el-form-item>
        <el-form-item label="鼠标" size="small">
          <el-input type="text" v-model="teamRow.mouse"></el-input>
        </el-form-item>
        <el-form-item label="键盘" size="small">
          <el-input type="text" v-model="teamRow.keyword"></el-input>
        </el-form-item>
        <el-form-item label="其他" size="small">
          <el-input type="text" v-model="teamRow.other"></el-input>
        </el-form-item>
        <el-form-item prop="computerBuy" label="购买年份" size="small">
          <el-input type="number" v-model.number="teamRow.computerBuy"></el-input>
        </el-form-item>
        <el-form-item label="办公配置照片" size="small">
          <input type="file" ref='workPhoto' accept="image/*" @change="fileUpload($event,'workPhoto')">
        </el-form-item>
        <el-form-item label="电脑配套照片" size="small">
          <input type="file" ref='computerPhoto' accept="image/*" @change="fileUpload($event,'computerPhoto')">
        </el-form-item>
        <el-form-item label="系统照片" size="small">
          <input type="file" ref='systemPhoto' accept="image/*" @change="fileUpload($event,'systemPhoto')">
        </el-form-item>
        <el-form-item label="备注说明" size="small">
          <el-input type="textarea" v-model="teamRow.remark"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="imgMsg.label" :visible.sync="imgVisible" width="600px">
      <img class="img-dialog" :src="imgMsg.src" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import {
    getList,
    addAssetUse,
    selectAssetUse,
    deleteAssetUse,
    updateAssetUse,
    selectOperator
  } from '@/api/assetusage'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const checkDate = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const filePath = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
      <a v-if="scope.row[colConfig.prop]" @click="openVisible(colConfig.label,scope.row[colConfig.prop])">查看
      </a>
      <a v-else style="color: red">X</a>
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'checkDate', label: '盘点日期', width: '150', fixed: true, component: checkDate },
          { prop: 'teamName', label: '部门', width: '150', fixed: true },
          { prop: 'user', label: '使用人', width: '150', fixed: true },
          { prop: 'officeTable', label: '办公桌', width: '150' },
          { prop: 'officeChair', label: '办公椅', width: '150' },
          { prop: 'displayer', label: '显示器', width: '150' },
          { prop: 'displayerTwo', label: '显示器2', width: '150' },
          { prop: 'computerMain', label: '主机', width: '150' },
          { prop: 'systemVersion', label: '系统版本', width: '150' },
          { prop: 'memory', label: '内存', width: '150' },
          { prop: 'hd', label: '硬盘', width: '150' },
          { prop: 'cpu', label: 'CPU', width: '150' },
          { prop: 'mouse', label: '鼠标', width: '150' },
          { prop: 'keyword', label: '键盘', width: '150' },
          { prop: 'other', label: '其他', width: '150' },
          { prop: 'computerBuy', label: '电脑购买年份', width: '150' },
          { prop: 'workPhotoFile', label: '办公配置照片', width: '150', component: filePath },
          { prop: 'computerPhotoFile', label: '电脑配套照片', width: '150', component: filePath },
          { prop: 'systemPhotoFile', label: '系统照片', width: '150', component: filePath },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        propertyNoList: [],
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增资产使用情况',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          checkDate: [
            { required: true, message: '请选择盘点日期', trigger: 'change' }
          ],
          user: [
            { required: true, message: '请输入使用人', trigger: 'change' }
          ],
          officeTable: [
            { required: true, message: '请输入办公桌', trigger: 'change' }
          ],
          officeChair: [
            { required: true, message: '请输入办公椅', trigger: 'change' }
          ],
          computerBuy: [
            { required: true, message: '请输入电脑购买年份', trigger: 'change' }
          ]
        },
        visible: false,
        imgVisible: false,
        imgMsg: {}
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        console.log(this.searchFormList)
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        this.outerVisible = true
        this.dialogTitle = '新增资产使用情况'
        this.fileClear()
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.teamRow.isTender = 1
        this.dialogType = 1
      },
      editTeam(assetsId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改资产使用情况'
        this.dialogType = 0
        this.fileClear()
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          assetsId: assetsId
        }
        selectAssetUse(params).then(response => {
          const data = response.responseBody
          this.teamRow = Object.assign({}, this.teamRow, data)
        })
      },
      submitForm(formName) {
        const params = new FormData()
        params.append('checkDate', this.teamRow.checkDate)
        params.append('teamId', this.teamRow.teamId)
        params.append('teamName', this.teamRow.teamName)
        params.append('user', this.teamRow.user)
        params.append('officeTable', this.teamRow.officeTable)
        params.append('officeChair', this.teamRow.officeChair)
        params.append('displayer', this.teamRow.displayer || '')
        params.append('displayerTwo', this.teamRow.displayerTwo || '')
        params.append('computerMain', this.teamRow.computerMain || '')
        params.append('systemVersion', this.teamRow.systemVersion || '')
        params.append('memory', this.teamRow.memory || '')
        params.append('hd', this.teamRow.hd || '')
        params.append('cpu', this.teamRow.cpu || '')
        params.append('mouse', this.teamRow.mouse || '')
        params.append('keyword', this.teamRow.keyword || '')
        params.append('other', this.teamRow.other || '')
        params.append('computerBuy', this.teamRow.computerBuy)
        params.append('workPhoto', this.teamRow.workPhoto)
        params.append('computerPhoto', this.teamRow.computerPhoto)
        params.append('systemPhoto', this.teamRow.systemPhoto)
        if (!this.dialogType) {
          params.append('assetsId', this.teamRow.assetsId)
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addAssetUse(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateAssetUse(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          assetsId: scope.row.assetsId,
          isDel: 1
        }
        deleteAssetUse(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      openVisible(msg) {
        this.imgVisible = true
        this.imgMsg = msg
      },
      fileClear() {
        if (this.$refs.workPhoto) {
          this.$refs.workPhoto.value = ''
        }
        if (this.$refs.computerPhoto) {
          this.$refs.computerPhoto.value = ''
        }
        if (this.$refs.systemPhoto) {
          this.$refs.systemPhoto.value = ''
        }
      },
      fileUpload(event, teamRowName) {
        this.$set(this.teamRow, teamRowName, event.target.files[0])
      },
      querySearchUser(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        if (queryString) {
          const params = {
            name: queryString
          }
          selectOperator(params).then(response => {
            for (const i of response.responseBody) {
              if (i.tertiarySector) {
                i.teamName = i.tertiarySector
              } else {
                let obj = {}
                obj = global.secondarySectorList.find((items) => {
                  return items.value === i.secondarySector // 筛选出匹配数据
                })
                i.teamName = obj.label
              }
              i.value = i.teamName + ' ' + i.name
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectUser(item) {
        this.$set(this.teamRow, 'user', item.name)
        this.$set(this.teamRow, 'teamId', item.teamId)
        this.$set(this.teamRow, 'teamName', item.teamName)
      }
    },
    mounted() {
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .img-dialog {
      max-width: 540px;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
